package com.wyh.controller.g3controller;

import com.wyh.common.result.Result;
import com.wyh.domain.po.g3po.G3Organization;
import com.wyh.domain.po.g3po.G3Project;
import com.wyh.service.g3service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api(tags = "分层结构")
@RestController
@RequestMapping("/Project")
@Validated
@Component("g3ProjectController")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    @ApiOperation("查找园区")
    @GetMapping("/findPark")
    public Result<List<Map<String, Object>>> findPark() {
        List<G3Project> g3Projects = projectService.findPark();

        List<Map<String, Object>> result = new ArrayList<>();
        for (G3Project g3Project : g3Projects) {
            Map<String, Object> projectMap = new HashMap<>();
            projectMap.put("Id", g3Project.getId());
            projectMap.put("superiorId", g3Project.getSuperiorid());
            projectMap.put("name", g3Project.getName());
            result.add(projectMap);
        }

        return Result.success(result);
    }

    @ApiOperation("查找楼盘")
    @GetMapping("/findFloor")
    public Result<List<Map<String, Object>>> findFloor() {
        List<G3Project> g3Projects = projectService.findFloor();

        List<Map<String, Object>> result = new ArrayList<>();
        for (G3Project g3Project : g3Projects) {
            Map<String, Object> projectMap = new HashMap<>();
            projectMap.put("Id", g3Project.getId());
            projectMap.put("superiorId", g3Project.getSuperiorid());
            projectMap.put("name", g3Project.getName());
            result.add(projectMap);
        }

        return Result.success(result);
    }
    @ApiOperation("查找资源")
    @GetMapping("/findbuildingunits")
    public Result<List<Map<String, Object>>> findbuildingunits() {
        List<G3Organization> g3Organizations = projectService.findorganization();

        List<Map<String, Object>> result = new ArrayList<>();
        for (G3Organization g3Organization : g3Organizations) {
            Map<String, Object> projectMap = new HashMap<>();
            projectMap.put("Id", g3Organization.getId());
            projectMap.put("parentId", g3Organization.getSuperiorid());
            projectMap.put("name", g3Organization.getName());
            result.add(projectMap);
        }

        return Result.success(result);
    }


    @ApiOperation("层级")
    @GetMapping("/getComplexHierarchy")
    public Result<List<Map<String, Object>>> getComplexHierarchy() {
        List<G3Organization> g3Organizations = projectService.findorganization();
        List<G3Project> parks = projectService.findPark();
        List<G3Project> buildings = projectService.findFloor();

        List<Map<String, Object>> hierarchyList = new ArrayList<>();

        for (G3Organization g3Organization : g3Organizations) {
            List<Map<String, Object>> sonList = new ArrayList<>();

            for (G3Organization subResource : g3Organizations) {
                if (Objects.equals(subResource.getSuperiorid(), g3Organization.getId()) && !isAlreadyAdded(subResource, sonList)) {
                    List<Map<String, Object>> parkList = new ArrayList<>();

                    for (G3Project park : parks) {
                        if (Objects.equals(park.getSuperiorid(), subResource.getId())) {
                            List<Map<String, Object>> buildingList = new ArrayList<>();

                            for (G3Project building : buildings) {
                                if (Objects.equals(building.getSuperiorid(), park.getId())) {
                                    Map<String, Object> buildingMap = new HashMap<>();
                                    buildingMap.put("key", "0-0-0-" + building.getId());
                                    buildingMap.put("parentId", building.getSuperiorid());
                                    buildingMap.put("name", building.getName());
                                    buildingMap.put("type", "build");

                                    buildingList.add(buildingMap);
                                }
                            }

                            if (!buildingList.isEmpty()) {
                                Map<String, Object> parkMap = new HashMap<>();
                                parkMap.put("key", "0-0-" + park.getId());
                                parkMap.put("parentId", park.getSuperiorid());
                                parkMap.put("name", park.getName());
                                parkMap.put("children", buildingList);
                                parkMap.put("type", "park");
                                parkList.add(parkMap);
                            }
                        }
                    }

                    if (!parkList.isEmpty()) {
                        Map<String, Object> sonMap = new HashMap<>();
                        sonMap.put("key", "0-" + subResource.getId());
                        sonMap.put("parentId", subResource.getSuperiorid());
                        sonMap.put("name", subResource.getName());
                        sonMap.put("children", parkList);
                        sonMap.put("type", "sonOrganization");

                        sonList.add(sonMap);
                    }
                }
            }

            if (!sonList.isEmpty()) {
                Map<String, Object> parentMap = new HashMap<>();
                parentMap.put("key", g3Organization.getId());
                parentMap.put("parentId", g3Organization.getSuperiorid());
                parentMap.put("name", g3Organization.getName());
                parentMap.put("children", sonList);
                parentMap.put("type", "organization");

                hierarchyList.add(parentMap);
            }
        }

        return Result.success(hierarchyList);
    }

    // 判断资源是否已经添加过
    private boolean isAlreadyAdded(G3Organization resource, List<Map<String, Object>> sonList) {
        for (Map<String, Object> map : sonList) {
            if (map.get("key").equals("0-" + resource.getId())) {
                return true;
            }
        }
        return false;
    }




}
